## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005
##
##  AlterReligion by Bhruic
##

from CvPythonExtensions import *
import CvUtil
import PyHelpers
import Popup as PyPopup

# globals
gc = CyGlobalContext()

class MigrantDestination:
	def __init__(self):
		self.city = None

	def doUnitBuilt(self, argsList):
		#print "Migrant Script v1.1"
		unit = argsList[1]
		if (unit.getUnitType() != gc.getInfoTypeForString("UNIT_MIGRANT")):
			return
		#print "Migrant Built"
		
		self.city = argsList[0]
		if (self.city):
			name = self.city.getName()
			if (name == None): 
				name = "Not_In_City"
			if (name == ""):
				name = "Not_In_City"
			unit.setScriptData(CvUtil.convertToStr(name))
		else:
			unit.setScriptData("Not_In_City")
		if (name != "Not_In_City"):
			pop = self.city.getPopulation()
			#print "City population is: ", pop
			pop = pop - 1
			self.city.setPopulation(pop)

		#print "Migrant Created: " + unit.getScriptData()


	def doUnitMove(self, argsList):
		#print "Migrant Script v1.1"
		name = "Not_In_City"
		plot, unit = argsList
		if (unit.getUnitType() != gc.getInfoTypeForString("UNIT_MIGRANT")):
			return
	
		self.city = plot.getPlotCity()
		#print "City ID: ",
		#print self.city
		if (self.city):
			name = self.city.getName()
			if (name == None): 
				name = "Not_In_City"
			if (name == ""):
				name = "Not_In_City"
			unit.setScriptData(CvUtil.convertToStr(name))
		else:
			unit.setScriptData("Not_In_City")
		#print "Migrant moved to city: " + unit.getScriptData()

	def doUnitLost(self, argsList):
		#print "Migrant Script v1.1"
		unit = argsList[0]
		if (unit.getUnitType() != gc.getInfoTypeForString("UNIT_MIGRANT")):
			return
		unit = argsList[0]
		migrantLocation = unit.getScriptData()
		#print "Disbanding Unit.  Current Location is " + migrantLocation

		if (migrantLocation == "Not_In_City"):
			#print "Unit disbanded outside of city"
			return

		player = gc.getPlayer(unit.getOwner())
		totalcities = player.getNumCities()
		currentcityIndex = 0
		searchDone = 0
		city = None
		while searchDone == 0:
			city = player.getCity(currentcityIndex)
			if (migrantLocation == city.getName()):
				self.city = city
				#print "Unit disbanded in city: " + self.city.getName()
				pop = self.city.getPopulation()
				#print "City population is: ", pop
				pop = pop + 1
				self.city.setPopulation(pop)
				#print "City population increased to: ", pop
				searchDone = 1	
			else:
				currentcityIndex = currentcityIndex + 1
				if (currentcityIndex >= totalcities):
					searchDone = 1
					#print "Unit disbanded in foreign city."
		return
				



